#!/bin/bash

#
# This job handles restarting the mirrorlist process and making sure all is well.
#

## ports for mirrorlist processes
mirrorlist1="http://localhost:18081/metalink?repo=rawhide&arch=x86_64"
mirrorlist2="http://localhost:18082/metalink?repo=rawhide&arch=x86_64"

TIME_DRAIN=30
TIME_RESTART=5
TIME_DISABLE=5

check_and_restart() {
	systemctl start $1
	if [[ ${?} -ne 0 ]]; then
		systemctl start $1
		if [[ ${?} -ne 0 ]]; then
			echo "Unable to start $1"
			exit 1
		fi
	fi
}

if [ ! -f /srv/mirrorlist/data/mirrorlist2/global_netblocks.txt ];
then
	cp /srv/mirrorlist/data/mirrorlist1/* /srv/mirrorlist/data/mirrorlist2/
fi

## check mirrorlist1 running
if [ `systemctl show mirrorlist1 -p ActiveState` != 'ActiveState=active' ]; then
	# mirrorlist1 not running, there is a problem
	echo "Error: mirrorlist1 is not running as expected. Trying to restart"
	check_and_restart mirrorlist1
fi

## check mirrorlist2 running
if [ `systemctl show mirrorlist2 -p ActiveState` != 'ActiveState=active' ]; then
	# mirrorlist2 not running, maybe a new install
	systemctl start mirrorlist2
	touch /srv/mirrorlist/data/mirrorlist1/mirrorlist_cache.proto
fi

## Check that protbuf cache is newer than old protobuf cache
if [ /srv/mirrorlist/data/mirrorlist1/mirrorlist_cache.proto -nt /srv/mirrorlist/data/mirrorlist2/mirrorlist_cache.proto ]; then
	# new proto
        :
else
	# No new proto
        exit 0
fi

# check mirrorlist2 (old protbuf cache and see that it's processing ok)
curl -q -H mirrors.fedoraproject.org ${mirrorlist2} -s -f --retry 50 --retry-delay 10 --retry-connrefused --retry-max-time 180 | grep "sha512" >/dev/null
if [ $? != 0 ]; then
	echo "ERROR: mirrorlist2 not processing correctly"
	exit 1
fi

# Drain mirrorlist1. This is safe since we assured that local2 is serving
echo "enable server mirror-lists-backend/mirrorlist-local2" | nc -U /var/run/haproxy-admin >& /dev/null
echo "set server mirror-lists-backend/mirrorlist-local1 state drain" | nc -U /var/run/haproxy-admin >& /dev/null
sleep ${TIME_DRAIN}
echo "disable server mirror-lists-backend/mirrorlist-local1" | nc -U /var/run/haproxy-admin >& /dev/null
sleep ${TIME_DISABLE}

# restart mirrorlist1 (new protbuf cache and make sure it's processing ok)
systemctl stop mirrorlist1
sleep 1
check_and_restart mirrorlist1


sleep ${TIME_RESTART}
curl -q -H mirrors.fedoraproject.org ${mirrorlist1} -s -f --retry 50 --retry-delay 10 --retry-connrefused --retry-max-time 180 | grep "sha512" >/dev/null
if [ $? != 0 ]; then
	echo "ERROR: mirrorlist1 did not restart correctly"
	exit 1
fi

# New mirrorlist seems to be working, put it back into service
echo "enable server mirror-lists-backend/mirrorlist-local1" | nc -U /var/run/haproxy-admin >& /dev/null
sleep ${TIME_RESTART}

# copy new protbuf cache to mirrorlist2
cp -a /srv/mirrorlist/data/mirrorlist1/* /srv/mirrorlist/data/mirrorlist2/

# Drain mirrorlist2
echo "set server mirror-lists-backend/mirrorlist-local2 state drain" | nc -U /var/run/haproxy-admin >& /dev/null
sleep ${TIME_DRAIN}
echo "disable server mirror-lists-backend/mirrorlist-local2" | nc -U /var/run/haproxy-admin >& /dev/null
sleep ${TIME_DISABLE}

# restart mirrorlist2
systemctl stop mirrorlist2
sleep 1
check_and_restart mirrorlist2

sleep ${TIME_RESTART}
curl -q -H mirrors.fedoraproject.org ${mirrorlist2} -o/dev/null -s -f --retry 50 --retry-delay 10 --retry-connrefused --retry-max-time 180
if [ $? != 0 ]; then
	echo "ERROR: mirrorlist2 did not restart correctly"
	exit 1
fi

echo "enable server mirror-lists-backend/mirrorlist-local2" | nc -U /var/run/haproxy-admin >& /dev/null
